package com.example.service.Impl;

import com.example.entity.Question;
import com.example.mapper.QuestionMapper;
import com.example.service.QuestionService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;

@Service
public class QuestionServiceImpl implements QuestionService {

    @Autowired
    private QuestionMapper questionMapper;

    @Override
    public PageInfo<Question> getAllQuestions(int pageNum, int pageSize) {
        // 设置分页参数
        PageHelper.startPage(pageNum, pageSize);
        // 查询数据
        List<Question> questions = questionMapper.selectAllQuestions();
        // 返回分页信息
        return new PageInfo<>(questions);
    }

    @Override
    public List<Integer> getQuestionOverview() {
        int totalQuestions = questionMapper.getTotalQuestions();
        int solvedQuestions = questionMapper.getSolvedQuestions();
        int pendingQuestions = questionMapper.getPendingQuestions();
        int processingQuestions = questionMapper.getProcessingQuestions();

        List<Integer> list = new ArrayList<>();
        list.add(totalQuestions);
        list.add(solvedQuestions);
        list.add(pendingQuestions);
        list.add(processingQuestions);
        return list;
    }

    @Override
    public Map<String, Integer> getQuestionCategory() {
        Map<String, Integer> categoryStats = new HashMap<>();
        categoryStats.put("page", questionMapper.getCategoryCount("1"));
        categoryStats.put("technical", questionMapper.getCategoryCount("2"));
        categoryStats.put("knowledge", questionMapper.getCategoryCount("3"));
        categoryStats.put("other", questionMapper.getCategoryCount("4"));
        return categoryStats;
    }

    @Override
    public Map<String, Object> getQuestionTrend() {
        // 获取按月分组的数据
        List<Map<String, Object>> trendData = questionMapper.getQuestionTrend();

        // 构造返回的数据结构
        Map<String, Object> trend = new HashMap<>();
        trend.put("trendData", trendData);

        return trend;
    }

    @Override
    public boolean submitQuestion(Question question) {
        // 插入问题数据到数据库
        int result = questionMapper.insertQuestion(question);
        return result > 0;
    }

}
